" 演示VimL虚拟机中的List内建函数

" 创建一个列表
let mylist = [1, 2, 3, 4, 5]
echo "Original list: " . string(mylist)

" 测试len函数
echo "Length of list: " . len(mylist)

" 测试add函数
call add(mylist, 6)
echo "After adding 6: " . string(mylist)

" 测试insert函数
call insert(mylist, 0, 0)  " 在索引0处插入0
echo "After inserting 0 at index 0: " . string(mylist)

call insert(mylist, 10)  " 在开头插入10
echo "After inserting 10 at beginning: " . string(mylist)

" 测试index函数
let idx = index(mylist, 3)
echo "Index of 3: " . idx

let idx2 = index(mylist, 10, 1)  " 从索引1开始查找10
echo "Index of 10 starting from index 1: " . idx2

" 测试count函数
call add(mylist, 3)
call add(mylist, 3)
echo "List with more 3s: " . string(mylist)
echo "Count of 3: " . count(mylist, 3)

" 测试remove函数
let removed = remove(mylist, 0)  " 移除索引0的元素
echo "Removed element: " . removed
echo "List after removal: " . string(mylist)

" 测试reverse函数
call reverse(mylist)
echo "Reversed list: " . string(mylist)

" 测试sort函数
call sort(mylist)
echo "Sorted list: " . string(mylist)

" 测试copy函数
let copylist = copy(mylist)
echo "Copied list: " . string(copylist)

" 修改原列表，检查是否是浅拷贝
call add(mylist, 100)
echo "Original list after adding 100: " . string(mylist)
echo "Copy list (should be unchanged): " . string(copylist)